package com.ndp.fb.rdb.mongo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.*;
import com.mongodb.util.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ndp.fb.constant.MongoConstants;
import com.ndp.fb.mongo.api.EcTemplateCreateMongoService;
import com.ndp.fb.mongo.model.EcTemplateCreate;
import com.ndp.fb.mongo.model.Feed;
import com.ndp.fb.rdb.model.EcMaterielElements;
import com.ndp.fb.rdb.model.EcTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hexin on 2016/5/31.
 */
@Service
public class EcTemplateCreateMongoServiceImpl implements EcTemplateCreateMongoService {

    @Autowired
    private DB mongoDBClientPrimary;

    /**
     * {
     * mongodbId：xxxx ,
     * feedList:[{
     * image_link：""
     * }],
     * originalFeedList:[{}]
     * status:1，
     * originalFeedSum:100,
     * createdFeedSum:20,
     * ecTemplate:{},
     * ecMaterielElementses:[]
     * }
     *
     * 实体类：EcMaterielJob
     *
     * @param mongodbId
     * @param feeds
     */
    @Override
    public void startCreate(String mongodbId, List<Feed> feeds, EcTemplate ecTemplate, List<EcMaterielElements> ecMaterielElementses) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_ECMATERIELJOB);


        List<DBObject> tempFeeds = new ArrayList<>();
        for (Feed feed : feeds) {
            DBObject o =  (DBObject)JSON.parse(com.alibaba.fastjson.JSON.toJSONString(feed));
            tempFeeds.add(o);
        }

        DBObject dbObject = new BasicDBObject();
        dbObject.put("mongodbId", mongodbId);
        dbObject.put("feedList", new ArrayList<>());
        dbObject.put("originalFeedList", tempFeeds);
        dbObject.put("status", 1);
        dbObject.put("originalFeedSum", feeds.size());
        dbObject.put("createdFeedSum", 0);
        dbObject.put("createTime", new Date());
        dbObject.put("createUTCTime", new Date().getTime());
        dbObject.put("ecTemplate", JSON.parse(com.alibaba.fastjson.JSON.toJSONString(ecTemplate)));
        dbObject.put("ecMaterielElementses", JSON.parse(com.alibaba.fastjson.JSON.toJSONString(ecMaterielElementses)));
        dbCollection.save(dbObject);
    }

    @Override
    public void pushNewFeed(String mongodbId, Feed feed) {

        DBObject o =  (DBObject)JSON.parse(com.alibaba.fastjson.JSON.toJSONString(feed));

        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_ECMATERIELJOB);
        DBObject query = new BasicDBObject();
        query.put("mongodbId", mongodbId);
        DBObject update = new BasicDBObject();
        update.put("$inc", new BasicDBObject("createdFeedSum", 1));
        update.put("$push", new BasicDBObject("feedList", o));
        dbCollection.update(query,update);
    }

    @Override
    public void updateFinished(String mongodbId) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_ECMATERIELJOB);
        DBObject query = new BasicDBObject();
        query.put("mongodbId", mongodbId);
        DBObject update = new BasicDBObject();
        update.put("$set", new BasicDBObject("status", 2));
        dbCollection.update(query,update);
    }

    @Override
    public EcTemplateCreate getCreate(String mongodbId) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_ECMATERIELJOB);
        DBObject q = new BasicDBObject();
        q.put("mongodbId", mongodbId);
        DBCursor dbCursor = dbCollection.find(q);

        EcTemplateCreate ecTemplateCreate = null;
        if (dbCursor != null) {
            while (dbCursor.hasNext()) {
                DBObject obj = dbCursor.next();
                ecTemplateCreate =  JSONObject.parseObject(JSONObject.toJSONString(obj), EcTemplateCreate.class);
                break;
            }
        }
        return ecTemplateCreate;
    }
}
